package com.ytkj.ygAssist.viewControl;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

import com.ytkj.ygAssist.main.AssistServer;
import com.ytkj.ygAssist.main.YunGouServer;
import com.ytkj.ygAssist.server.util.FilesUtil;
import com.ytkj.ygAssist.server.util.MyStringUtil;
import com.ytkj.ygAssist.tools.Config;
import com.ytkj.ygAssist.tools.JFrameListeningInterface;
import com.ytkj.ygAssist.view.MainJFrame;
import com.ytkj.ygAssist.view.UserLogin;
import com.ytkj.ygAssist.view.myView.CheckUpdate;
import com.ytkj.ygAssist.view.myView.ExpiringHint;
import com.ytkj.ygAssist.view.myView.HintDialog;

public class UserLoginControl extends UserLogin {
	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					init();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/*
	 * 初始化助手
	 */
	private static void init() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
		FilesUtil.readerConfigFile();
		try {
			new File("智能云购助手.exe").delete();
			new File("智能云购助手_cache.exe").renameTo(new File("智能云购助手.exe"));
		} catch (Exception e) {
		}
		try {
			Config.SystemAddressCode = MyStringUtil.getMACAddress() + System.currentTimeMillis();
			if (!Config.isServerVersions) {
				String softDownloadUrl = AssistServer.getSoftVersions();
				if (softDownloadUrl != null) {
					CheckUpdate.startUpdate(softDownloadUrl);
					return;
				}
				AssistServer.getHeziQQqunInfo();
			}
		} catch (Exception e) {
		}
		startUserLogin();
	}

	public static void startUserLogin() {
		UserLoginControl frame = new UserLoginControl();
		frame.setVisible(true);
		// 获取屏幕大小
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setLocation((screenSize.width - frame.getWidth()) / 2, (screenSize.height - frame.getHeight()) / 3);
	}

	public UserLoginControl() {
		super();
		setListening();
		addButtonMouseListener();
		initUserLogin();
	}

	private void initUserLogin() {
		if (!Config.isServerVersions) {
			new Timer().schedule(new TimerTask() {
				@Override
				public void run() {
					HintDialog.startExpiringHint(AssistServer.getCommonality().replace("__", "\r\n"));
				}
			}, 2000);
		}
		if (autologonCheckBox.isSelected()) {
			setButtonEnabled(false);
			loginButton.setText("取消登录");
			label_2.setText("正在自动登录...");
			new Timer().schedule(new TimerTask() {
				@Override
				public void run() {
					YunGouServer.loginAssist(userNameTextField.getText().trim(), new String(userPasswordField.getPassword()).trim(),
							foreknowInterface);
				}
			}, 2000);
		}
	}

	/*
	 * 添加按钮监听事件
	 */
	private void addButtonMouseListener() {
		loginButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				loginButton();
			}
		});
		userPasswordField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == 10) {
					loginButton();
				}
				super.keyReleased(e);
			}
		});
	}

	private void setListening() {
		foreknowInterface = new JFrameListeningInterface() {
			@Override
			public void setFrameText(String FrameName, String text) {
				if (FrameName.equals("loginError")) {
					label_2.setText(text);
					loginButton.setText("登    录");
					setButtonEnabled(true);
				} else if (FrameName.equals("loginSeriousnessError")) {
					loginButton.setText("登    录");
					label_2.setText("登录异常");
					setButtonEnabled(true);
					JOptionPane.showMessageDialog(UserLoginControl.this, "请检查用户名或密码，或者检查网络", "提示",
							JOptionPane.WARNING_MESSAGE);
				} else if (FrameName.equals("loginSucceed")) {
					if (loginButton.getText().equals("取消登录")) {
						if (remembCheckBox.isSelected()) {
							AssistServer.saveLoginAssistInfo(userNameTextField.getText().trim(),
									new String(userPasswordField.getPassword()).trim());
						} else {
							AssistServer.saveLoginAssistInfo(userNameTextField.getText().trim(), "");
						}
						FilesUtil.saveUserInfo("\\loginConfiguration.log", "" + remembCheckBox.isSelected(),
								"" + autologonCheckBox.isSelected());
						Config.userName = userNameTextField.getText().trim();
						Config.userPassword = new String(userPasswordField.getPassword()).trim();
						FilesUtil.readerGoodsInfoFile();
						MainJFrame.startMainJFrame();
						UserLoginControl.this.removeAll();
						UserLoginControl.this.setVisible(false);
					}
				} else if (FrameName.equals("loginErrorOverdue")) {
					label_2.setText(text);
					label_5.setVisible(true);
					lblqq_1.setVisible(true);
					loginButton.setText("登    录");
					setButtonEnabled(true);
					ExpiringHint.startExpiringHint(2);
				}
			}
		};
	}

	/*
	 * 设置按钮
	 */
	private void setButtonEnabled(boolean isTrue) {
		userNameTextField.setEnabled(isTrue);
		userPasswordField.setEnabled(isTrue);
		remembCheckBox.setEnabled(isTrue);
		autologonCheckBox.setEnabled(isTrue);
	}

	private void loginButton() {
		if (loginButton.getText().equals("登    录")) {
			if (userNameTextField.getText().equals("") || userNameTextField.getText().equals("请使用一元云购账户登录")
					|| new String(userPasswordField.getPassword()).equals("")) {
				label_2.setText("请输入用户名或密码");
			} else {
				setButtonEnabled(false);
				loginButton.setText("取消登录");
				label_2.setText("正在登录...");
				YunGouServer.loginAssist(userNameTextField.getText().trim(), new String(userPasswordField.getPassword()).trim(),
						foreknowInterface);
			}
		} else {
			loginButton.setText("登    录");
			label_2.setText("");
			setButtonEnabled(true);
		}
	}
}
